package com.zp.base.impl.serialNumberDict;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.base.entity.serialNumberDict.SerialNumberDict;
import com.zp.base.entity.serialNumberDict.bo.SerialNumberDictBo;
import com.zp.base.entity.serialNumberDict.vo.SerialNumberDictVo;
import com.zp.base.mapper.serialNumberDict.SerialNumberDictMapper;
import com.zp.base.service.serialNumberDict.ISerialNumberDictService;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * 业务系统单号、序号字典Service业务层处理
 *
 * @author zhang peng
 * @ date 2023-07-07
 */
@RequiredArgsConstructor
@Service
public class SerialNumberDictServiceImpl implements ISerialNumberDictService {

    private final SerialNumberDictMapper baseMapper;

    /**
     * 查询业务系统单号、序号字典
     */
    @Override
    public SerialNumberDictVo queryById (String noId) {
        return baseMapper.selectVoById(noId);
    }

    /**
     * 获取单条记录
     *
     * @param bo
     * @return
     */
    @Override
    public SerialNumberDictVo queryOne (SerialNumberDictBo bo) {
        return baseMapper.queryOne(bo);
    }

    /**
     * 查询业务系统单号、序号字典列表(分页)
     */
    @Override
    public TableDataInfo<SerialNumberDictVo> queryPageList (SerialNumberDictBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SerialNumberDict> lqw = buildQueryWrapper(bo);
        Page<SerialNumberDictVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询业务系统单号、序号字典列表(不分页)
     */
    @Override
    public List<SerialNumberDictVo> queryList (SerialNumberDictBo bo) {
        LambdaQueryWrapper<SerialNumberDict> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SerialNumberDict> buildQueryWrapper (SerialNumberDictBo bo) {
        LambdaQueryWrapper<SerialNumberDict> lqw = Wrappers.lambdaQuery();
        lqw.eq(ObjectUtil.isNotNull(bo.getClinicNo()), SerialNumberDict::getClinicNo, bo.getClinicNo());
        lqw.eq(ObjectUtil.isNotNull(bo.getHospitalNo()), SerialNumberDict::getHospitalNo, bo.getHospitalNo());
        lqw.eq(ObjectUtil.isNotNull(bo.getDrugImportNo()), SerialNumberDict::getDrugImportNo, bo.getDrugImportNo());
        lqw.eq(ObjectUtil.isNotNull(bo.getDrugExportNo()), SerialNumberDict::getDrugExportNo, bo.getDrugExportNo());
        lqw.eq(ObjectUtil.isNotNull(bo.getConsumablesImportNo()), SerialNumberDict::getConsumablesImportNo, bo.getConsumablesImportNo());
        lqw.eq(ObjectUtil.isNotNull(bo.getConsumablesExportNo()), SerialNumberDict::getConsumablesExportNo, bo.getConsumablesExportNo());
        lqw.eq(StringUtils.isNotBlank(bo.getOrgId()), SerialNumberDict::getOrgId, bo.getOrgId());
        lqw.eq(bo.getCurrentNewDate() != null, SerialNumberDict::getCurrentNewDate, bo.getCurrentNewDate());
        return lqw;
    }

    /**
     * 新增业务系统单号、序号字典
     */
    @Override
    @Transactional
    public SerialNumberDictBo insertByBo (SerialNumberDictBo bo) {
        SerialNumberDict add = BeanUtil.toBean(bo, SerialNumberDict.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insertInfo(add) > 0;
        if (flag) {
            bo.setNoId(add.getNoId());
        }
        return bo;
    }

    /**
     * 修改业务系统单号、序号字典
     */
    @Override
    @Transactional
    public SerialNumberDictBo updateByBo (SerialNumberDictBo bo) {
        SerialNumberDict update = BeanUtil.toBean(bo, SerialNumberDict.class);
        validEntityBeforeSave(update);
        baseMapper.updateById(update);
        return bo;
    }

    @Override
    @Transactional
    public SerialNumberDictBo updateInfo (SerialNumberDictBo bo) {
        SerialNumberDict update = BeanUtil.toBean(bo, SerialNumberDict.class);
        baseMapper.updateInfo(update);
        return bo;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave (SerialNumberDict entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除业务系统单号、序号字典
     */
    @Override
    public Boolean deleteWithValidByIds (Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 修改对应的申请序号
     * @param lambdaUpdateWrapper
     * @return
     */
    @Override
    @Transactional
    public Boolean updateSerialNumbers (LambdaUpdateWrapper<SerialNumberDict> lambdaUpdateWrapper) {
        return baseMapper.update(lambdaUpdateWrapper) > 0;
    }
}
